package GUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Listeners.AssignSubchairListener;
import Listeners.DownloadListener;
import Model.Conference;
import Model.Paper;
import Model.User;

/**
 * This class is used to create all of the papers's decision status for a
 * particular conference. This class is similar to PaperPanel except it is used
 * solely by the Program Chair.
 * 
 * @author Irving Estrera
 * */
@SuppressWarnings("serial")
public class PaperStatusPanel extends JPanel
{
  public static final String ACCEPTED = "Accepted";
  public static final String REJECTED = "Rejected";
  public static final String UNDECIDED = "Undecided";
  private static final int RECOMMENDATION = 1;
  private static final int PAPER = 0; //Not used (for download button)
  private JLabel my_status_label;
  private ConferenceWizardFrame my_frame;
  private User my_logged_in;
  private Conference my_conference;

  /**
   * The constructor. Sets up all paper components on to the panel.
   * 
   * @param the_conference
   *          The conference
   * @param the_user
   *          The program chair
   * @param the_paper
   *          The associated conference paper
   * @param the_frame
   *          The current frame this panel is placed on
   * */
  public PaperStatusPanel(final Conference the_conference, final User the_user,
      final Paper the_paper, final ConferenceWizardFrame the_frame)
  {
    super();
    setAlignmentX(LEFT_ALIGNMENT);
    GridLayout gLayout = new GridLayout(0, 2);
    setLayout(gLayout);
    JLabel titleLabel = new JLabel("Title: " + the_paper.getTitle());
    titleLabel.setFont(new Font("Plain", Font.PLAIN, 18));
    titleLabel.setAlignmentX(LEFT_ALIGNMENT);

    JLabel author_label = new JLabel("Author: "
        + the_paper.getAuthor().realName());
    author_label.setFont(new Font("Plain", Font.PLAIN, 16));
    author_label.setForeground(new Color(0, 0, 102));

    my_status_label = getLabel(the_paper);
    my_status_label.setForeground(Color.RED);
    my_status_label.setFont(new Font("Serif", Font.PLAIN, 20));
    my_frame = the_frame;
    my_logged_in = the_user;
    my_conference = the_conference;

    JPanel status_panel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
    JButton decision_button = createDecisionButton(the_paper);
    my_status_label.setText(my_status_label.getText());
    status_panel.add(my_status_label);
    status_panel.add(decision_button);

    JPanel button_panel = new JPanel(new GridLayout(0, 2));
    JButton download_recmd_button = new JButton("Recommendation");
    download_recmd_button.addActionListener(new DownloadListener(the_paper,
        RECOMMENDATION));
//    JButton download_paper_button = new JButton("Download Manuscript");
//    download_paper_button.addActionListener(new DownloadListener(the_paper, PAPER));

    button_panel.setLayout(new FlowLayout(FlowLayout.TRAILING));
    button_panel.add(download_recmd_button);
//    button_panel.add(download_paper_button);

    add(titleLabel);
    add(status_panel);
    add(author_label);
    add(button_panel);

    JButton assign_button = new JButton("Assign");
//    assign_button.addActionListener(new AssignSubChairButtonListener(
//        my_conference, my_logged_in, the_paper, my_frame));
    assign_button.addActionListener(
        new AssignSubchairListener(my_conference, the_paper, my_logged_in, my_frame));

    JLabel subchair_label = new JLabel();
    if (the_paper.isAssigned())
    {
      subchair_label.setText("Assigned to "
          + the_paper.getSubprogramChair().realName());
    } else
    {
      subchair_label.setText("NOT ASSIGNED");
    }

    JPanel subchair_status_panel = new JPanel(
        new FlowLayout(FlowLayout.LEADING));
    subchair_status_panel.add(assign_button);
    subchair_status_panel.add(subchair_label);

    add(subchair_status_panel);
    // this.setPreferredSize();
    setMaximumSize(new Dimension(800, 80));
    setBorder(BorderFactory.createLineBorder(Color.BLACK));
  }

  /**
   * This method creates a decision button for an associated paper.
   * 
   * @param the_paper
   *          the associated paper
   * @return The button
   * */
  private JButton createDecisionButton(final Paper the_paper)
  {
    JButton decision_button = new JButton("Edit Decision");
    final List<String> decisions = new ArrayList<String>();
    decisions.add(UNDECIDED);
    decisions.add(ACCEPTED);
    decisions.add(REJECTED);
    decision_button.addActionListener(new AbstractAction()
    {
      @Override
      public void actionPerformed(final ActionEvent the_event)
      {
        String result = (String) JOptionPane.showInputDialog(null,
            "Set decision", "status", JOptionPane.PLAIN_MESSAGE, null,
            decisions.toArray(), my_status_label.getText());
        if (result != null)
        {
          if (result.equals(UNDECIDED))
          {
            the_paper.editDecision(Conference.UNDECIDED);
            my_frame.setCurrentPanel(new ProgramChairPanel(my_conference,
                my_logged_in, my_frame));
          } else if (result.equals(ACCEPTED))
          {
            the_paper.editDecision(Conference.ACCEPTED);
            my_frame.setCurrentPanel(new ProgramChairPanel(my_conference,
                my_logged_in, my_frame));
          } else if (result.equals(REJECTED))
          {
            the_paper.editDecision(Conference.REJECTED);
            my_frame.setCurrentPanel(new ProgramChairPanel(my_conference,
                my_logged_in, my_frame));
          }
        }
      }
    });
    return decision_button;
  }

  /**
   * Returns a label of the associated paper's current decision status.
   * 
   * @param the_paper
   *          The associated paper
   * @return The status label
   * */
  private JLabel getLabel(final Paper the_paper)
  {
    JLabel decision_label = new JLabel();
    switch (the_paper.getDecision())
    {
    case Conference.ACCEPTED:
      decision_label.setText(ACCEPTED);
      break;
    case Conference.REJECTED:
      decision_label.setText(REJECTED);
      break;
    case Conference.UNDECIDED:
      decision_label.setText(UNDECIDED);
      break;
    default:
      throw new IllegalArgumentException(the_paper.getTitle()
          + " was assigned an" + " invalid int. " + the_paper.getDecision()
          + " Decision should either be" + " 0, -1, or 1");
    }
    return decision_label;
  }
}
